<?php

namespace Coldwind;

class Curl {

    /**
     * 请求接口，返回数据
     * @param string $url 接口地址
     * @param mixed $param 传递的参数
     * @param bool $isPost 是否以POST提交，默认GET
     * @return object
     */
    public static function request($url, $param = false, $isPost = 0) {
        if (is_array($param)) {
            /* 拼装参数字符串 */
            $paramStr = Url::assembleParam($param);
        } else {
            $paramStr = $param;
        }

        $httpInfo = array();
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($isPost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paramStr);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($paramStr) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $paramStr);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === FALSE) {
            #echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }

}
